package com.xuxukang.springboot.demo.mvc;

import cn.hutool.json.JSONObject;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.jarvis.core.Jarvis;
import java.io.IOException;
import java.util.Set;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xuxukang
 * @date 2019-07-05 09:58
 */
@RestController
public class TestController {

  private static final Log log = LogFactory.get();

  private static HttpClient httpClient = HttpClients.createDefault();
  private static OkHttpClient okHttpClient = new OkHttpClient();

  @Autowired
  StringRedisTemplate stringRedisTemplate;

  @GetMapping("/test")
  public Object test() {
//    JarvisUtil jarvisUtil = new JarvisUtil();
//    jarvisUtil.test();
//    jarvisUtil.hello();
//    JarvisUtil.testStatic();

    return new JSONObject().set("success", true);
  }

  @GetMapping("/pay/unifiedorder")
  public String mockWxHttp() {
    return new JSONObject().set("data", "mock").toString();
  }

  @GetMapping("/testDataRedis")
  public Object testDataRedis() {
    stringRedisTemplate.opsForValue().set("key", "value");
    String value = stringRedisTemplate.opsForValue().get("key");
    System.out.println(value);

    stringRedisTemplate.opsForHash().put("hash", "key1", "value1");
    Object value1 = stringRedisTemplate.opsForHash().get("hash", "key1");
    System.out.println(value1);

    stringRedisTemplate.opsForList().leftPush("key2", "value2");
    String value2 = stringRedisTemplate.opsForList().leftPop("key2");
    System.out.println(value2);

    stringRedisTemplate.opsForSet().add("key3", "value3");
    Set<String> value3 = stringRedisTemplate.opsForSet().members("key3");
    System.out.println(value3);

    // uninstall plugins
    Jarvis.INSTANCE.stop();

    stringRedisTemplate.opsForValue().set("key4", "value4");
    String value4 = stringRedisTemplate.opsForValue().get("key4");
    System.out.println(value);

    // restart plugin
    Jarvis.INSTANCE.restartPlugin("springboot-dataredis-prefix");

    stringRedisTemplate.opsForValue().set("key5", "value5");
    String value5 = stringRedisTemplate.opsForValue().get("key5");
    System.out.println(value);

    return new JSONObject().put("success", true);
  }

  @GetMapping("/testWxMock")
  public Object testWxMock() throws IOException {
    /*HttpResponse execute = httpClient.execute(new HttpGet("https://api.mch.weixin.qq.com/pay/unifiedorder"));
    int statusCode = execute.getStatusLine().getStatusCode();
    log.info("[status]{}", statusCode);
    if (statusCode == 200) {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      execute.getEntity().writeTo(out);
      String responseString = out.toString();
      out.close();
      log.info("[responseString]{}", responseString);
    }*/

    Request request = new Request.Builder().url("https://api.mch.weixin.qq.com/pay/unifiedorder")
        .build();
    Response execute1 = okHttpClient.newCall(request).execute();
    int code = execute1.code();
    log.info("[status1]{}", code);
    if (code == 200) {
      String result = execute1.body().string();
      log.info("[execute1 result]{}", result);
    }

    return new JSONObject().set("success", true);
  }

}
